package edu.hawaii.halealohacli.processor;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import javax.xml.datatype.XMLGregorianCalendar;

import org.junit.Test;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Test class for Processor.
 * 
 * @author Russell Vea for ICS 314 Fall 2011
 * @author Branden Ogata
 */
public class TestProcessor {

  private WattDepotClient client = 
      new WattDepotClient("http://server.wattdepot.org:8190/wattdepot/"); 
  private Processor processor;
  
  /**
   * Instantiates a processor and passes a test (with dates reversed).
   * @throws Exception upon error with the server.
   */
  public TestProcessor() throws Exception {
    this.processor = new Processor(this.client);
    this.processor.process("Ilima 2011-12-02 2011-11-30");
  }
  
  /**
   * Test to see if the source is not null.
   */

  @Test public void testGetSource() {
    Source test = this.processor.getSource();
    assertNotNull("", test.getSubSources());
    assertEquals("Test to see if name is 'Ilima'", "Ilima", test.getName());
  }

  
  /**
   * Tests the beginning timestamp stored in processor.
   */
  @Test public void testGetTimestamp() {
    XMLGregorianCalendar tstamp = this.processor.getTimestamp();
    assertEquals("Make sure day is 30", 30, tstamp.getDay());
    assertEquals("Make sure month is 11", 11, tstamp.getMonth());
    assertEquals("Make sure year is 2011", 2011, tstamp.getYear());
  }


  /**
   * Tests the beginning timestamp stored in processor.
   * Makes sure that timestamp is before end timestamp.
   */
   @Test public void testGetBeginningTimestamp() {
    XMLGregorianCalendar begin = this.processor.getBeginningTimestamp();
    XMLGregorianCalendar end = this.processor.getEndTimestamp();
    
    assertEquals("Check to see if day is 30", 30, begin.getDay());
    assertEquals("Check to see if month is December - 1 month", 11, begin.getMonth());
    assertEquals("Check to see if month is 2011", 2011, begin.getYear());
    assertTrue("Check to see if timestamp is before other timestamp",
        begin.toGregorianCalendar().before(end.toGregorianCalendar()));
  }

 
  /**
   * Tests the end timestamp stored in processor.
   * Makes sure that timestamp is after end timestamp.
   */
  @Test public void testGetEndTimestamp() {
    XMLGregorianCalendar end = this.processor.getEndTimestamp();
    assertEquals("Make sure the day for the end timestamp is 2.", 2 , end.getDay());
    assertEquals("Make sure the month for the end timestamp is December.", 12, end.getMonth());
    assertEquals("Make sure the year for the end timestamp is 2011.", 2011 , end.getYear());
    assertTrue("Make sure end timestamp is after beginning timestamp",
        this.processor.getEndTimestamp().toGregorianCalendar().
        after(this.processor.getBeginningTimestamp().toGregorianCalendar()));
  }

}
